<?php
   //                                                                                      S 
  //                                                                                        I
 /////////////////////////////////////////////////////////////\                         A    M Y
//                                                           //\                         D    P
//     License:                                              // \      /\             \/ |\/| |
//     This is free open source software,                    //  \    /  \            /\ |  | |__
//     There is no warranty on it,                           //   \  /    \                I    E
//     It is delviered as it is,                             //    \/      \                N                        /\
//     You can modify it to tailor your needs,               //             \                                       /  \      !
//     You can change license of your copy as you wish.      //              \                ________version_0.1__/    \    /
//     Copyleft: supertramp@wasthere.com                     //               \              /                           \  / 
//                                                           //                \            /                             \/
//////////////////////////////////////////////////////////////                  \          / 
//                                                                               \  /\    /
// config constants:                                                              \/  \  / 
//                                                                                     \/
define('SERVER_SIDE_LOG_DIR', dirname(__FILE__).'/logs/');
define('RUN_CAPTION', 'Run');
define('RUN_PARAM', 'jdf39mf8x3fnhyi8we');
define('DATE_FORMAT', '-Y-m-d_H:i:s');
define('LOG_FILE_EXT', '.log');
//
class MySimpleXmlAdmin extends MySimpleXmlCms {
 //
  private $sServerSideLogDir;
  private $sLogFile = null;
  //
  // run xproc if given in POST
  public function __construct($sServerSideContentDir = SERVER_SIDE_CONTENT_DIR, $sServerSideLogDir = SERVER_SIDE_LOG_DIR){
    parent::__construct($sServerSideContentDir);
    $this->sServerSideLogDir = $sServerSideLogDir;
    if(isset($_POST[RUN_PARAM])){
      $this->run($_POST[RUN_PARAM]);
    }
  }
  //
  // returns html for control panel to run transformations
  public function getPanel($sXprocXmlFilesPattern){
    $sReturn .= '<form action="?" method="post">';
    $sReturn .= '<select name="'.RUN_PARAM.'">';
    $aXprocs = glob($this->sServerSideContentDir.$sXprocXmlFilesPattern);
    foreach($aXprocs as $sXproc){
      $aData = $this->getData(basename($sXproc));
      $sReturn .= '<option value="'.basename($sXproc).'">'.$aData['PROC']['NAME'].'</option>';
    }
    $sReturn .= '</select>';
    $sReturn .= '<input type="submit" value="'.RUN_CAPTION.'" />';
    $sReturn .= '</form>';
    if($this->sLogFile){
      $sReturn .= '<textarea>';
      $sReturn .= file_get_contents($this->sLogFile);
      $sReturn .= '</textarea>';
    }
    return $sReturn;
  }
  //
  // run transformation implementation
  private function run($sXmlXprocFileName){
    $aData = $this->getData($sXmlXprocFileName);
    $this->sLogFile = SERVER_SIDE_LOG_DIR.$aData['PROC']['NAME'].date(DATE_FORMAT, time()).LOG_FILE_EXT;
    $aFiles = glob($aData['PROC']['SRC_PATH']);
    //
    file_put_contents($this->sLogFile, "Log filename: ".$this->sLogFile."\n", FILE_APPEND);
    file_put_contents($this->sLogFile, "Xproc filename: ".$this->sServerSideContentDir.$sXmlXprocFileName."\n", FILE_APPEND);
    file_put_contents($this->sLogFile, "Source path: ".$aData['PROC']['SRC_PATH']."\n", FILE_APPEND);
    file_put_contents($this->sLogFile, "Destination path: ".$aData['PROC']['DEST_PATH']."\n- - -\n", FILE_APPEND);
    file_put_contents($this->sLogFile, "Using XSD:\n- - -\n".$aData['PROC']['XSD']."\n- - -\n", FILE_APPEND);
    file_put_contents($this->sLogFile, "Using XSLT:\n- - -\n".$aData['PROC']['XSLT']."\n- - -\n", FILE_APPEND);
    //
    foreach($aFiles as $sFileName){
      file_put_contents($this->sLogFile, "XML input file: ".$sFileName."\n", FILE_APPEND);
      $xml= new DOMDocument();
      $xml->load($sFileName); 
      // validate with XSD:
      if (!$xml->schemaValidateSource($aData['PROC']['XSD'])){
        file_put_contents($this->sLogFile, "ERROR: XML not valid with XSD!\n", FILE_APPEND);
      } else {
        file_put_contents($this->sLogFile, "XSD validation passed\n", FILE_APPEND);
        // apply XSLT transformation:
        $xslt = new XSLTProcessor(); 
        $XSL = new DOMDocument(); 
        $XSL->loadXML($aData['PROC']['XSLT']); 
        $xslt->importStylesheet($XSL); 
        file_put_contents($aData['PROC']['DEST_PATH'].basename($sFileName), $xslt->transformToXML( $xml )); 
        file_put_contents($this->sLogFile, "XSLT output file: ".$aData['PROC']['DEST_PATH'].basename($sFileName)."\n", FILE_APPEND);
      }
      file_put_contents($this->sLogFile, "- - -\n", FILE_APPEND);
    }
  }
}

?>